/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v1.1 copyright Comine.com 20101211S22
#include "MStdLib.h"
#include "TList.h"
#include "TVector.h"
#include "MIrrlicht.h"
#include "MCGLCameraControl.h"


//******************************************************
//**  Module Elements
//******************************************************


//******************************************************
//**  MCGLCameraControl class
//******************************************************
void MCGLCameraControl::ClearObject(void)
	{
	}


////////////////////////////////////////////////
MCGLCameraControl::MCGLCameraControl(bool create)
	{
	ClearObject();
	if(create==true && Create()==false)
		{
		return;
		}
	}


////////////////////////////////////////////////
MCGLCameraControl::~MCGLCameraControl(void)
	{  Destroy();  }


////////////////////////////////////////////////
bool MCGLCameraControl::Create(void)
	{
	Destroy();

	if(mMap.Create()==false)
		{
		Destroy();
		return false;
		}

	// Create a default key map like irrlict engine

	SKeyMap *newdata=0;

	// Add Up Key
	if(mMap.Add(newdata)==false)
		{
		Destroy();
		return false;
		}

	newdata->Action=EKA_MOVE_FORWARD;
	newdata->KeyCode=KEY_UP;

	// Add Down Key
	if(mMap.Add(newdata)==false)
		{
		Destroy();
		return false;
		}

	newdata->Action=EKA_MOVE_BACKWARD;
	newdata->KeyCode=KEY_DOWN;

	// Add Left Key Key
	if(mMap.Add(newdata)==false)
		{
		Destroy();
		return false;
		}

	newdata->Action=EKA_STRAFE_LEFT;
	newdata->KeyCode=KEY_LEFT;

	// Add Right Key
	if(mMap.Add(newdata)==false)
		{
		Destroy();
		return false;
		}

	newdata->Action=EKA_STRAFE_RIGHT;
	newdata->KeyCode=KEY_RIGHT;
		
	return true;
	}


////////////////////////////////////////////////
bool MCGLCameraControl::Destroy(void)
	{
	mMap.Destroy();
	ClearObject();
	return true;
	}


////////////////////////////////////////////////
bool MCGLCameraControl::Clear(void)
	{
	if(mMap.Create()==false)
		{
		return false;
		}

	return true;
	}
	

////////////////////////////////////////////////
bool MCGLCameraControl::Remove(EKEY_ACTION action)
	{
	SKeyMap *p;
	mMap.ReadReset();
	while(mMap.Read(p)==true)
		{
		if(p->Action!=action)
			{
			continue;
			}

		mMap.Remove(p);
		return true;
		}
	
	return true;
	}


////////////////////////////////////////////////
bool MCGLCameraControl::Add(EKEY_ACTION action,EKEY_CODE keycode)
	{
	Remove(action);

	SKeyMap *newdata=0;

	// Add Up Key
	if(mMap.Add(newdata)==false)
		{
		Destroy();
		return false;
		}

	newdata->Action=action;
	newdata->KeyCode=keycode;
	
	return true;
	}


//////////////////////////////////////////////////
bool MCGLCameraControl::GetKeyArray(TVector<SKeyMap> &vectarray)
	{
	const int count=mMap.GetCount();
	if(count==0)
		{
		return false;
		}
	
	if(vectarray.Create(count)==false)
		{
		return false;
		}

	SKeyMap *p;
	mMap.ReadReset();
	
	for(int i=0;mMap.Read(p)==true;++i)
		{
		vectarray[i].Action=p->Action;
		vectarray[i].KeyCode=p->KeyCode;
		}

	return true;
	}

